package cn.kx.flutter_kx_crypto;

import android.annotation.TargetApi;
import android.os.Build;
import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;


public class Crypto3DES {


    /**
     *
     * @param key
     *            24 位密钥
     * @param str
     *            源字符串
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws UnsupportedEncodingException
     * @throws InvalidKeySpecException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static byte[] encode(String str, String key)
            throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException,
            InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException {

        byte[] newkey = key.getBytes();

        SecureRandom sr = new SecureRandom();
        DESedeKeySpec dks = new DESedeKeySpec(newkey);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
        byte[] bt = cipher.doFinal(str.getBytes("utf-8"));

        return bt;
    }

    /**
     * 解密
     *
     * @param edata
     * @param key
     * @return
     * @throws Exception
     */
    public static String decode(byte[] edata, String key) {
        String data = "";
        try {
            byte[] newkey = key.getBytes();
            DESedeKeySpec dks = new DESedeKeySpec(newkey);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey securekey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, securekey, new SecureRandom());
            byte[] bb = cipher.doFinal(edata);
            data = new String(bb, "UTF-8");

        } catch (Exception e) {
            return e.getLocalizedMessage();
        }
        return data;
    }


    /**
     * BASE64 加密
     *
     * @param key
     * @return
     * @throws Exception
     */
    @TargetApi(Build.VERSION_CODES.FROYO)
    public static String BASE64Encrypt(byte[] key) {
        String edata = null;
        try {
            edata = Base64.encodeToString(key,Base64.DEFAULT);
            return  edata;
        } catch (Exception e) {
            e.getLocalizedMessage();
        }
        return edata.replaceAll("\r|\n", "");
    }

    /**
     * BASE64 解密
     *
     * @param data
     * @return
     * @throws Exception
     */
    @TargetApi(Build.VERSION_CODES.FROYO)
    public static byte[] BASE64Decrypt(String data) {

        byte[] edata = null;
        try {
            edata = Base64.decode(data, Base64.DEFAULT);
        } catch (Exception e) {
            e.getLocalizedMessage();
        }
        return edata;
    }

}
